#URL = "http://www.pyside.org/files/shiboken-%(VERSION)s.tar.bz2"
URL = "https://distfiles.macports.org/py-shiboken/shiboken-%(VERSION)s.tar.bz2"

VERSIONS = ["1.0.0", "1.0.1", "1.0.2", "1.0.3", "1.1.2"]
DEFAULT_VERSION = "1.1.2"
#DEPENDS = ["jom", "qt4osmin", "apiextractor", "generatorrunner", "python" ]
DEPENDS = ["jom", "qt4osmin", "python" ]

# TODO Fix backward compatibility.
# pyside-1.1.1 depends on qt-4.8.0.
# pyside-1.1.1 depends on shiboken-1.1.1
# pyside-1.1.1 is coupled with pyside_tools-0.2.13.
#
# Since pyside-1.1.1, dependencies to apiextractor and generatorrunner
# have been removed.
#
# Older versions of pyside (1.0.*) may no longer work because of this dep.


#
#note, builder script on gitorious:
#http://qt.gitorious.org/pyside/packaging/blobs/master/windows/createpackage.js
#
#
#note:
#CMake Warning at CMakeLists.txt:80 (message):
#  Compiling shiboken with debug enabled, but the python executable was not
#  compiled with debug support.
#

from source.builders import CMakeJomBuilder, CMakeBuilder
import os

#class shibokenBuilder( CMakeJomBuilder ):
class shibokenBuilder( CMakeBuilder ):
  """ subclass of CMakeBuilder with custom configure step """

  def hasdebug( self ):
    return False

  def setgenerator(self):
    """default generator using machine.buildarch"""
    if self.machine.isLinux():
      CMakeBuilder.setgenerator(self)
    else:
      self.generator = "NMake Makefiles"

  def configure(self):
    if "jom" in self.dependencies.keys(): 
      self.dependencies["jom"].addexetopath()
    qtdir = self.dependencies['qt4osmin'].targetpath()
    os.environ['QTDIR'] = qtdir
    #os.environ['ApiExtractor_DIR'] = self.dependencies['apiextractor'].targetpath()
    #os.environ['GeneratorRunner_DIR'] = self.dependencies['generatorrunner'].targetpath()
    
    shibokendlldir = os.sep.join([self.extractedpath,"libshiboken",("Release","Debug")[self.debug]])
    #generatordir = os.sep.join([self.extractedpath,"generator",("Release","Debug")[self.debug]])

    #put our qt in the start of the path, else another qt dll, possible 32-bit will prevent generatorrunner from running
    os.environ['PATH'] =  os.pathsep.join([os.environ['QTDIR']+os.sep+"bin",
                                           #os.environ['ApiExtractor_DIR']+os.sep+"bin",
                                           #os.environ['GeneratorRunner_DIR']+os.sep+"bin",
                                           shibokendlldir,
                                           #generatordir,
                                           os.environ['PATH']])

    #define PYTHON_DEBUG_LIBRARY, PYTHON_EXECUTABLE, PYTHON_INCLUDE_DIR, PYTHON_LIBRARY
    pythonbindir= self.dependencies["python"].getbindir()
    pythonlibdir =  self.dependencies["python"].getlibdir() + ("s", "" )[self.machine.isLinux()]
    pythonincludedir = self.dependencies["python"].getincludedir()

    pyversionshort = self.dependencies["python"].libversionshort()
    pyversion = self.dependencies["python"].libversion()

    pythonlib = ("python%s.lib" % pyversionshort, "libpython%s.so" % pyversion)[self.machine.isLinux()]
    pythondebuglib = ("python%s_d.lib" % pyversionshort, "libpython%s_d.so" % pyversion)[self.machine.isLinux()]

    incdir = " -DPYTHON_INCLUDE_DIR=" + pythonincludedir
    incdir += " -DPYTHON_INCLUDE_DIR2=" + pythonincludedir

    #todo fix getting version from dep
    lib = " -DPYTHON_LIBRARY="+pythonlibdir+os.sep+pythonlib
    libdebug = " -DPYTHON_DEBUG_LIBRARY="+pythonlibdir+os.sep+pythondebuglib

    pythonexe = (("python.exe", "python_d.exe")[self.debug], ("python", "python")[self.debug])[self.machine.isLinux()]
    exe = " -DPYTHON_EXECUTABLE="+pythonbindir+os.sep+pythonexe

    notests = " -DBUILD_TESTS=FALSE"

    debugpostfix = r" -DCMAKE_DEBUG_POSTFIX='_d'"
    additional_args = incdir+lib+libdebug+exe+notests+debugpostfix

    if self.machine.isLinux():
      self.cleandir( self.extractedpath + os.sep + "build" )
      os.chdir( self.extractedpath + os.sep + "build" )
      self.setgenerator()
      self.execute( "cmake .. "+additional_args+" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=" + self.installpath()  + " -G\"" + self.generator + "\"")
    else:
      #CMakeJomBuilder.configure(self, additional_args)
      CMakeBuilder.configure(self, additional_args)

  def make(self):
    if self.machine.isLinux():
      os.chdir( self.extractedpath + os.sep + "build" )
      self.makeUnixMakefiles()
    else:
      #CMakeJomBuilder.make(self)
      CMakeBuilder.make(self)


BUILDER = shibokenBuilder
